// 此文件作用：启动静态web服务，1.访问dist目录，2.启动本地代理

var express = require('express')
var axios = require('axios')
var config = require('./config/index')

var app = express()
var apiRoutes = express.Router()

// 路由：代理
apiRoutes.get('/proxy', (req, res) => {
  req.query.url = unescape(req.query.url)

  return axios.get(req.query.url, {
    headers: {
      Origin: 'https://c.y.qq.com',
      Referer: 'https://c.y.qq.com/',
      Host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
  })
})

// 配置 /api 子路由
app.use('/api', apiRoutes)

// 打包编译后的目录 作为静态资源目录
app.use(express.static('./dist'))

// 监听端口
var port = process.env.PORT || config.build.port

module.exports = app.listen(port, function(err) {
  if (err) {
    console.log(err)
  }
  console.log('Listening at http://localhost:' + port + '\n')
})
